package com.CusterRelSys.control;

import com.CusterRelSys.entity.SysPrivilege;
import com.CusterRelSys.entity.SysUser;
import com.CusterRelSys.service.SysUserServiceCBH;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class SysUserControlCBH {
    @Resource
    SysUserServiceCBH sysUserServiceCBH;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(HttpSession session, SysUser sysUser) {
        sysUser = sysUserServiceCBH.checkLogin(sysUser.getUsrName(), sysUser.getUsrPassword());
        if (sysUser != null) {
            session.setAttribute("sysUser", sysUser);
            return "redirect:/main.jsp";
        }
        return "redirect:/login.jsp";
    }

    @RequestMapping("/getPriviege")
    @ResponseBody
    public List<SysPrivilege> getUserRole(HttpSession session){
        SysUser sysUser = (SysUser)session.getAttribute("sysUser");
        if(sysUser!=null){
            return sysUserServiceCBH.selectuserPriviege(sysUser.getUsrId());
        }
        return null;
    }
}